$(function () {
    /**
     * 初始化资金记录列表
     */
    var initFundRecordTable = function () {

        var table = $('#fundRecordList').DataTable({
            "processing": true,
            "serverSide": true,
            "sort": false,
            "search": false,
            "ajax": {
                "url": "fund/getUserWithdrawRecord",
                "data": function (d) {
                    //d.fundStatus = fundStatus.val();
                },
                "type": "POST"
            },
            "columns": [
                {"data": "orderId"},
                {"data": "transId"},
                {"data": "priv"},
                {"data": "amount"},
                {"data": "type"},
                {"data": "operation"},
                {"data": "recordTime"},
                {"data": "status"},
                {"data": "id"}
            ],
            "aoColumnDefs": [{
                    "targets": 1,
                    "visible": false
                }, {
                    "targets": 2, //用户
                    "mRender": function (data, type, row) {
                        return '<a href="user/index/' + row.userId + '" target="_blank">' + data + '</a>';
                    }
                }, {
                    "targets": 3, //金额
                    "mRender": function (data, type, row) {
                        return $.number(data, 2);
                    }
                }, {
                    "targets": 4, //资金类型
                    "visible": false,
                    "mRender": function (data, type, row) {
                        return sysFundType[data];
                    }
                }, {
                    "targets": 5, //操作类型
                    "visible": false,
                    "mRender": function (data, type, row) {
                        return sysFundOperation[data];
                    }
                }, {
                    "targets": 7, //资金状态
                    "mRender": function (data, type, row) {
                        return sysFundStatus[data];
                    }
                }, {
                    "targets": 6, //时间
                    "mRender": function (data, type, row) {
                        return $.format.date(new Date(data), "yyyy/MM/dd HH:mm");
                    }
                }, {
                    "targets": 8,
                    "mRender": function (data, type, row) {
                        var buttons = $('#opButtons').html().replace(/#id/g, data);
                        if ('O' === row.status) {
                            buttons = $('#cancelButton').html().replace(/#id/g, data);
                        }
                        return buttons;
                    }
                }],
            "order": [6, 'desc'],
            "oLanguage": chinese,
            "iDisplayLength": 20,
            "aLengthMenu": [
                [5, 10, 20, 50, 100, -1],
                [5, 10, 20, 50, 100, "全部"]
            ],
            "sDom": 't<"dt-panelfooter clearfix"lp>'
        });

        function withdrawAudit(msg, btn) {
            var id = btn.data("id"), approve = btn.data("approve");
            sys.confirm(msg, function (btn) {
                var $btn = $(btn);
                sys.btnSpin($btn);
                sys.processModal();
                $.post("pnr/userWithdrawAudit", {
                    requestId: id,
                    approve: approve
                }, function (res) {
                    if (res.success) {
                        if (approve) {
                            alert("提现已受理");
                        } else {
                            alert("提现已拒绝");
                        }
                        location.reload();
                    } else {
                        if (approve) {
                            alert("提现批准失败");
                        } else {
                            alert("提现拒绝失败");
                        }
                        sys.btnStop($btn);
                        sys.closeModal();
                    }
                }).fail(function () {
                    alert("网络通信错误");
                    sys.btnStop($btn);
                    sys.closeModal();
                });
            });
        }

        function cancelWithdrawAudit(msg, b) {
            var id = b.data("id");
            sys.confirm(msg, function (btn) {
                var $btn = $(btn);
                sys.btnSpin($btn);
                sys.processModal();

                $.post("fund/cancelUserWithdrawAudit", {
                    requestId: id,
                }, function (res) {
                    alert(res.message);
                    if (res.success) {
                        location.reload();
                    } else {
                        sys.btnStop($btn);
                        sys.closeModal();
                    }
                    $.magnificPopup.close();
                }).fail(function () {
                    alert("网络通信错误");
                    sys.btnStop($btn);
                    sys.closeModal();
                });
            });
        }

        $('#tbody').on('click', '.approve-btn', function () {
            withdrawAudit("确定批准该提现？", $(this));
        });
        $('#tbody').on('click', '.reject-btn', function () {
            withdrawAudit("确定拒绝该提现？", $(this));
        });
        $('#tbody').on('click', '.cancel-btn', function () {
            cancelWithdrawAudit("请务必联系用户，确认提现金额已经到账后再进行操作。确定取消该提现？", $(this));
        });
    };

    initFundRecordTable();
});
